//
//  MysetViewController.m
//  BigTyre
//
//  Created by 张钧 on 2017/3/30.
//  Copyright © 2017年 Lemon. All rights reserved.
//

#import "MysetViewController.h"
#import "Common.h"
#import "MyCommon.h"
#import <ShareSDK/ShareSDK.h>

@interface MysetViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * table;
    NSArray * arr1;
    CGFloat size;
//    NSString *message;
}
@end

@implementation MysetViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=NO;
    self.tabBarController.tabBar.hidden=YES;

}
- (void)viewDidLoad {
    [super viewDidLoad];

    arr1=@[@"仅WIFI联网",@"消息提醒设置",@"去好评",@"功能反馈",@"关于大轮胎"];
    self.title=@"设置";
    table=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    table.delegate=self;
    table.dataSource=self;
    table.tableFooterView=[[UIView alloc]init];
    [self.view addSubview:table];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 1;
    }
    else if (section==1){
        return arr1.count;
    }
    else if (section==2)   {
        return 1;
    }
    return 0;
}
//表格分区头视图不显示的问题
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSArray * headtitlearr=@[@"",@"     大轮胎app在运行加载过程中，所产生的图片和视频等缓存，点击清除，让您的手机运行速度加快",@"",];
    UIView * headview=[[UIView alloc]init];
    UILabel * lab=[[UILabel alloc]initWithFrame:CGRectMake(15, 0,self.view.frame.size.width-20, 40)];
    lab.font=[UIFont systemFontOfSize:13];
    lab.numberOfLines=0;
    
    headview.backgroundColor=[UIColor colorWithRed:252/250.0 green:247/250.0 blue:245/250.0 alpha:1.0];
    lab.text=headtitlearr[section];
    [headview addSubview:lab];
    return headview;
}
- (CGFloat)tableView:(UITableView * )tableView heightForHeaderInSection:(NSInteger)section;
{
    //表格分区头视图高度
    if (section==0){
        return 10;
    }
    else if(section==1){
        return 45;
    }
    else if (section==2){
        return 70;
    }
    return 0;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:@"set"];
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    if (indexPath.section==0) {
                if (!cell) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"set"];
        }
        cell.textLabel.text=@"清空缓存";
        cell.imageView.image=[UIImage imageNamed:@"myset_clearcache"];
    }
    else if(indexPath.section==1)
    {
        if (!cell) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"set"];
        }
        cell.textLabel.text=arr1[indexPath.row];
        cell.detailTextLabel.text=@"＞";
        if (indexPath.row==0) {
            cell.detailTextLabel.hidden=YES;
            //开关按钮
            UISwitch * switc = [[UISwitch alloc]initWithFrame:CGRectMake((WIDTH-80), (45-30)/2, 60, 30)];
            switc.on = NO;
            [cell addSubview:switc];
        }
    }
    else if(indexPath.section==2)
    {
        if (!cell) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"set"];
        }
        cell.textLabel.font=[UIFont systemFontOfSize:19];
        cell.textLabel.text=@"退出登录";
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.textLabel.textAlignment=NSTextAlignmentCenter;
        cell.backgroundColor=[UIColor redColor];
        cell.textLabel.textColor=[UIColor whiteColor];
    }
    return cell;
}
-(CGFloat  )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //单元格高度
    if (indexPath.section==2) {     return 60;  }
    else{   return 45;  }
    return 0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        
        size = [self folderSizeAtPath:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject] + [self folderSizeAtPath:NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject] + [self folderSizeAtPath:NSTemporaryDirectory()];
      NSString *   message = size > 1 ? [NSString stringWithFormat:@"缓存%.2fM, 删除缓存", size] : [NSString stringWithFormat:@"缓存%.2fK, 删除缓存", size * 1024.0];

           NSLog(@"=======%.2f",size);

        UIAlertController  *alert=[UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *myCachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)lastObject];
            [self cleanCaches:myCachePath];
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [self.navigationController presentViewController:alert animated:NO completion:^{
            
        }];
    }
    if (indexPath.section==2) {
        [ShareSDK cancelAuthorize:SSDKPlatformTypeQQ];
        [ShareSDK cancelAuthorize:SSDKPlatformTypeWechat];
        [ShareSDK cancelAuthorize:SSDKPlatformTypeSinaWeibo];
        [ShareSDK cancelAuthorize:SSDKPlatformTypeFacebook];
        [self.navigationController popViewControllerAnimated:NO];
        
    }
}
// 计算目录大小
- (CGFloat)folderSizeAtPath:(NSString *)path{
    // 利用NSFileManager实现对文件的管理
    NSFileManager *manager = [NSFileManager defaultManager];
    CGFloat size1 = 0;
    if ([manager fileExistsAtPath:path]) {
        // 获取该目录下的文件，计算其大小
        NSArray *childrenFile = [manager subpathsAtPath:path];
        for (NSString *fileName in childrenFile) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            size1+= [manager attributesOfItemAtPath:absolutePath error:nil].fileSize;
        }
        // 将大小转化为M
        return size1 / 1024.0 / 1024.0;
    }
    return 0;
}
// 根据路径删除文件
-(void)cleanCaches:(NSString *)path{
    // 利用NSFileManager实现对文件的管理
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        // 获取该路径下面的文件名
        NSArray *childrenFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childrenFiles) {
            // 拼接路径
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            // 将文件删除
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }

}
@end
